id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

Collection TransformationsMap1

prev  |  next  |  chance

val doubleIt: (Int) -> Int = { i: Int -> i * 2 }

val tripleIt: (Int) -> Int = { it * 3 }

fun mapIt1Long(i: Int, func: (Int) -> Int): List<Int> {
  val list = mutableListOf<Int>()
  for (v in (0..i)) {   // (0..i) is inclusive
    val w = func.invoke(v)
    list.add(w)
  }
  return list
}

fun mapIt1Short(i: Int, func: (Int) -> Int): List<Int> = (0..i).map(func)

Function Call  Return Value
mapIt1Long(6, doubleIt)
mapIt1Long(6, tripleIt)
mapIt1Short(6, doubleIt)
mapIt1Short(6, tripleIt)
mapIt1Short(4) { i: Int -> i * 4 }
mapIt1Short(4) { i: Int -> i * 5 }
mapIt1Short(4) { it * 7 }
mapIt1Short(4) { it * 8 }

Experiment with this code on Gitpod.io or as a Kotlin Playground

⬅ Back